import pymysql
from flask import Flask, render_template
from markupsafe import escape

from config import mysql_config
from config.mysql_db import pool
from custom_logger.CustomLogger import CustomLogger
from dbsql.views import dbsql
from login.views import login_bp
from redirect.views import redirect_bp
from table.custom_models import db
from upload.upload import upload_bp
from user.views import user_bp
from table.views import db_user_bp

app = Flask(__name__)

# 创建自定义 logger 实例
custom_logger = CustomLogger('flask_logger')

# 将自定义 logger 存储到 Flask 应用程序的配置中
app.config['custom_logger'] = custom_logger
app.config['DB_POOL'] = pool
app.config['UPLOAD_FOLDER'] = '/Users/hujingbo/PycharmProjects/flaskProject/upload-file/'
app.config.from_object(mysql_config)

# 使用 pymysql 替代 MySQLdb
pymysql.install_as_MySQLdb()

# 初始化DB
db.init_app(app)

# 定义蓝图
app.register_blueprint(login_bp, custom_logger=custom_logger)
app.register_blueprint(user_bp)
app.register_blueprint(dbsql)
app.register_blueprint(redirect_bp)
app.register_blueprint(upload_bp)
app.register_blueprint(db_user_bp)


@app.route('/')
def hello_world():
    # 获取自定义 logger
    logger = custom_logger.get_logger()
    logger.info("app info log")
    logger.warning("app warning log")
    logger.error("app error log")
    return 'Hello World!'


@app.route("/<name>")
def hello_name(name):
    return f"Hello, {escape(name)}!"


@app.route('/hello/')
@app.route('/hello/<name>')
def hello_rander(name=None):
    return render_template('hello.html', name=name)


if __name__ == '__main__':
    app.run(debug=True)
